package cn.appsys.controller.backend;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.appsys.controller.developer.AppController;
import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.developer.AppCategoryService;
import cn.appsys.service.developer.AppInfoService;
import cn.appsys.service.developer.AppVersionService;
import cn.appsys.service.developer.DataDictionaryService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;

@RequestMapping("/manager/backend/app")
@Controller
public class AppCheckController {
	private Logger logger = Logger.getLogger(AppController.class);

	@Resource
	private AppInfoService appInfoService;

	@Resource
	private AppVersionService appVersionService;

	@Resource
	private AppCategoryService appCategoryService;

	@Resource
	private DataDictionaryService dataDictionaryService;

	@RequestMapping(value = "/list")
	public String getAppInfoList(Model model, HttpSession session,
			@RequestParam(value = "querySoftwareName", required = false) String _querySoftwareName,
			@RequestParam(value = "queryFlatformId", required = false) String _queryFlatformId,
			@RequestParam(value = "queryCategoryLevel1", required = false) String _queryCategoryLevel1,
			@RequestParam(value = "queryCategoryLevel2", required = false) String _queryCategoryLevel2,
			@RequestParam(value = "queryCategoryLevel3", required = false) String _queryCategoryLevel3,
			@RequestParam(value = "pageIndex", required = false) String pageIndex) {
		logger.debug("_querySoftwareName ======>" + _querySoftwareName);
		logger.debug("_queryFlatformId ======>" + _queryFlatformId);
		logger.debug("_queryCategoryLevel1 ======>" + _queryCategoryLevel1);
		logger.debug("_queryCategoryLevel2 ======>" + _queryCategoryLevel2);
		logger.debug("_queryCategoryLevel3 ======>" + _queryCategoryLevel3);
		logger.debug("pageIndex ======>" + pageIndex);

		// 将一些可能会导致后面转换值引发空指针异常的参数设置默认值
		if (_queryCategoryLevel1 == null || _queryCategoryLevel1 == "") {
			_queryCategoryLevel1 = "0";
		}
		if (_queryCategoryLevel2 == null || _queryCategoryLevel2 == "") {
			_queryCategoryLevel2 = "0";
		}
		if (_queryCategoryLevel3 == null || _queryCategoryLevel3 == "") {
			_queryCategoryLevel3 = "0";
		}
		if (_queryFlatformId == null || _queryFlatformId == "") {
			_queryFlatformId = "0";
		}

		// 设置页面容量
		int pageSize = Constants.pageSize;
		// 当前页码
		int currentPageNo = 1;
		// 对pageIndex的值进行简单处理
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				return "redirect:error.html";
			}
		}
		// 总数量（表）
		int totalCount = 0;
		try {
			totalCount = appInfoService.getAppInfoCount(_querySoftwareName, "1", _queryFlatformId, _queryCategoryLevel1,
					_queryCategoryLevel2, _queryCategoryLevel3, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PageSupport page = new PageSupport();
		page.setCurrentPageNo(currentPageNo);
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		// 控制首页和尾页
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalCount) {
			currentPageNo = totalCount;
			if (currentPageNo < 1) {
				currentPageNo = 1;
			}
		}
		List<AppInfo> appInfoList = null;
		List<DataDictionary> flatFormList = null;
		List<DataDictionary> statusList = null;
		List<AppCategory> categoryLevel1List = null;
		List<AppCategory> categoryLevel2List = null;
		List<AppCategory> categoryLevel3List = null;
		try {
			appInfoList = appInfoService.getAppInfoList(_querySoftwareName, "1", _queryFlatformId, _queryCategoryLevel1,
					_queryCategoryLevel2, _queryCategoryLevel3, pageSize, currentPageNo, null);

			flatFormList = getDataDictionaries("APP_FLATFORM");
			statusList = getDataDictionaries("APP_STATUS");
			categoryLevel1List = appCategoryService.getCategoryLevelList(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (_queryCategoryLevel2 != null && _queryCategoryLevel2 != "") {
				categoryLevel2List = getAppCategories(_queryCategoryLevel1.toString());
				model.addAttribute("categoryLevel2List", categoryLevel2List);
				if (_queryCategoryLevel3 != null && _queryCategoryLevel3 != "") {
					categoryLevel3List = getAppCategories(_queryCategoryLevel2.toString());
					model.addAttribute("categoryLevel3List", categoryLevel3List);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("appInfoList", appInfoList);
		model.addAttribute("flatFormList", flatFormList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("querySoftwareName", _querySoftwareName);
		model.addAttribute("queryFlatformId", _queryFlatformId);
		model.addAttribute("queryCategoryLevel1", _queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", _queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", _queryCategoryLevel3);
		model.addAttribute("pages", page);
		model.addAttribute("categoryLevel1List", categoryLevel1List);
		return "backend/applist";
	}

	/**
	 * 回调方法
	 * 
	 * @param pid
	 * @return
	 */
	public List<AppCategory> getAppCategories(String pid) {
		return appCategoryService.getCategoryLevelList(pid);
	}

	/**
	 * 回调方法
	 * 
	 * @param tcode
	 * @return
	 */
	public List<DataDictionary> getDataDictionaries(String tcode) {
		return dataDictionaryService.getDataDictionaryList(tcode);
	}

	@RequestMapping(value = "/categorylevellist.json", method = RequestMethod.GET)
	@ResponseBody
	public List<AppCategory> getCategoryLevellist(Model model, @RequestParam String pid) {
		if (pid == "") {
			pid = null;
		}
		return getAppCategories(pid);
	}

	@RequestMapping("/check")
	public String check(@RequestParam("aid") String aid, @RequestParam("vid") String vid, Model model) {
		AppInfo appInfo = null;
		AppVersion appVersion = null;
		try {
			appInfo = appInfoService.getAppInfo(Integer.parseInt(aid));
			appVersion = appVersionService.getAppVersion(aid, vid);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		model.addAttribute("appVersion", appVersion);
		model.addAttribute("appInfo", appInfo);
		return "backend/appcheck";
	}

	@RequestMapping("/checksave")
	public String checksave(AppInfo appInfo) {
		if (!appInfoService.updateAppStatus(appInfo.getStatus(), appInfo.getId())) {
			
		}
		return "redirect:/manager/backend/app/list";
	}

}
